<?php
require("top.ajax.php");

switch($_POST['method']){
    case 'checkCart':
        echo $_SESSION['cart']->checkCart();

        break;
    case 'addQuantity':
        $p=new Product($_POST['id_product']);
        $add=$_SESSION['cart']->addQuantity($p);
        $prices = array('quantity'=>$add['quantity'], "totalPrice"=>$add['totalPrice'], "subTotal"=>$_SESSION['cart']->recalculateSubTotal($add['idvendor']), "total"=>$_SESSION['cart']->getRecalculateTotal(), "idVendor"=>$add['idvendor']);
        echo json_encode($prices);
        break;

    case 'subQuantity':
        $p=new Product($_POST['id_product']);
        $sub=$_SESSION['cart']->subQuantity($p);
        if(isset($_SESSION['cart']['reduction_code'])) {
            $totalWS = $_SESSION['cart']->getRecalculateTotal(false);
            if($_SESSION['cart']['reduction_code']['list']['min_achat']>$totalWS) $prices = array('quantity'=>$sub['quantity'], "totalPrice"=>$sub['totalPrice'], "subTotal"=>$_SESSION['cart']->recalculateSubTotal($sub['idvendor']), "total"=>$_SESSION['cart']->getRecalculateTotal(), "idVendor"=>$sub['idvendor'],"error"=>'min_achat', "totalWS"=>$totalWS, "value"=>$_SESSION['cart']['reduction_code']['list']['reduction'], "min"=>$_SESSION['cart']['reduction_code']['list']['min_achat']);
        }
        if(isset($_SESSION['cart']['sponsorship'])) {
            $totalWS = $_SESSION['cart']->getRecalculateTotal(false);
            if($_SESSION['cart']->getMinAmount($_SESSION['cart']['sponsorship'])>$totalWS) $prices = array('quantity'=>$sub['quantity'], "totalPrice"=>$sub['totalPrice'], "subTotal"=>$_SESSION['cart']->recalculateSubTotal($sub['idvendor']), "total"=>$_SESSION['cart']->getRecalculateTotal(), "idVendor"=>$sub['idvendor'],"error"=>'min_achat', "totalWS"=>$totalWS, "value"=>$_SESSION['cart']['sponsorship'], "min"=>$_SESSION['cart']->getMinAmount($_SESSION['cart']['sponsorship']));
        }
        else $prices = array('quantity'=>$sub['quantity'], "totalPrice"=>$sub['totalPrice'], "subTotal"=>$_SESSION['cart']->recalculateSubTotal($sub['idvendor']), "total"=>$_SESSION['cart']->getRecalculateTotal(), "idVendor"=>$sub['idvendor'],"error"=>'false');

        echo json_encode($prices);
        break;

    case 'deleteProduct':
       $delVendor = $_SESSION['cart']->deleteProduct($_POST['id_vendor'], $_POST['id_product']);
       if(isset($_SESSION['cart']['reduction_code'])) {
            $totalWS = $_SESSION['cart']->getRecalculateTotal(false);
            if($_SESSION['cart']['reduction_code']['list']['min_achat']>$totalWS){
                $value=$_SESSION['cart']['reduction_code']['list']['reduction']; $min=$_SESSION['cart']['reduction_code']['list']['min_achat'];
                unset($_SESSION['cart']['reduction_code']);
                $datas = array("delVendor"=>$delVendor, "total"=>$_SESSION['cart']->getRecalculateTotal(), "sizeOfCart"=>sizeof($_SESSION['cart']['vendors']), "subTotal"=>$_SESSION['cart']->recalculateSubTotal($_POST['id_vendor']), "error"=>'min_achat', "totalWS"=>$totalWS, "value"=>$value, "min"=>$min);
            }
            else $datas = array("delVendor"=>$delVendor, "total"=>$_SESSION['cart']->getRecalculateTotal(), "sizeOfCart"=>sizeof($_SESSION['cart']['vendors']), "subTotal"=>$_SESSION['cart']->recalculateSubTotal($_POST['id_vendor']));

        }
        elseif(isset($_SESSION['cart']['sponsorship'])) {
            $totalWS = $_SESSION['cart']->getRecalculateTotal(false);
            if($_SESSION['cart']->getMinAmount($_SESSION['cart']['sponsorship'])>$totalWS){
                $value=$_SESSION['cart']['sponsorship']; $min=$_SESSION['cart']->getMinAmount($_SESSION['cart']['sponsorship']);
                unset($_SESSION['cart']['sponsorship']);
                $datas = array("delVendor"=>$delVendor, "total"=>$_SESSION['cart']->getRecalculateTotal(), "sizeOfCart"=>sizeof($_SESSION['cart']['vendors']), "subTotal"=>$_SESSION['cart']->recalculateSubTotal($_POST['id_vendor']), "error"=>'min_achat', "totalWS"=>$totalWS, "value"=>$value, "min"=>$min);
            }
            else $datas = array("delVendor"=>$delVendor, "total"=>$_SESSION['cart']->getRecalculateTotal(), "sizeOfCart"=>sizeof($_SESSION['cart']['vendors']), "subTotal"=>$_SESSION['cart']->recalculateSubTotal($_POST['id_vendor']));

        }
        else $datas = array("delVendor"=>$delVendor, "total"=>$_SESSION['cart']->getRecalculateTotal(), "sizeOfCart"=>sizeof($_SESSION['cart']['vendors']), "subTotal"=>$_SESSION['cart']->recalculateSubTotal($_POST['id_vendor']));
        echo json_encode($datas);
        break;

    case 'deleteVendor':
        $_SESSION['cart']->deleteVendor($_POST['id_vendor']);
        $res=array();
        for($i=0;$i<sizeof($_SESSION['cart']['vendors']);$i++){
                  if($_SESSION['cart']['vendors'][$i]['vendor']->isDelivering($_SESSION['cart']['country']['id'])==false) $res[$i]=$_SESSION['cart']['vendors'][$i]['vendor']['id'];
        }
        if(isset($_SESSION['cart']['reduction_code'])){            
            $totalWS = $_SESSION['cart']->getRecalculateTotal(false);
            if($_SESSION['cart']['reduction_code']['list']['min_achat']>$totalWS){
                $value=$_SESSION['cart']['reduction_code']['list']['reduction']; $min=$_SESSION['cart']['reduction_code']['list']['min_achat'];
                unset($_SESSION['cart']['reduction_code']);
                $datas = array("notdelivery"=>$res, "total"=>$_SESSION['cart']->getRecalculateTotal(), "sizeOfCart"=>sizeof($_SESSION['cart']['vendors']), "error"=>'min_achat', "totalWS"=>$totalWS, "value"=>$value, "min"=>$min);
            }
        }
        elseif(isset($_SESSION['cart']['sponsorship'])){
            $totalWS = $_SESSION['cart']->getRecalculateTotal(false);
            if($_SESSION['cart']->getMinAmount($_SESSION['cart']['sponsorship'])>$totalWS){
                $value=$_SESSION['cart']['sponsorship']; $min=$_SESSION['cart']->getMinAmount($_SESSION['cart']['sponsorship']);
                unset($_SESSION['cart']['sponsorship']);
                $datas = array("notdelivery"=>$res, "total"=>$_SESSION['cart']->getRecalculateTotal(), "sizeOfCart"=>sizeof($_SESSION['cart']['vendors']), "error"=>'min_achat', "totalWS"=>$totalWS, "value"=>$value, "min"=>$min);
            }
        }
        else $datas = array("notdelivery"=>$res, "total"=>$_SESSION['cart']->getRecalculateTotal(), "sizeOfCart"=>sizeof($_SESSION['cart']['vendors']));
        echo json_encode($datas);
        break;



    case 'changeCountry':
        $res='';
        if($_POST['id_country']==0) echo '';
        else {
            $country = new Country($_POST['id_country']);
            $_SESSION['cart']->changeCountry($country);
            for($i=0;$i<sizeof($_SESSION['cart']['vendors']);$i++){
                  if($_SESSION['cart']['vendors'][$i]['vendor']->isDelivering($country['id'])==false){
                      $res .= "<div class=\"notdelivery\">".$_SESSION['cart']['vendors'][$i]['vendor']." ne livre pas en ".$country['fr'].". <span class=\"deleteVendor\" id=\"deleteVendor".$_SESSION['cart']['vendors'][$i]['vendor']['id']."\"> Supprimer du panier les produits de ".$_SESSION['cart']['vendors'][$i]['vendor'].".</span></div>";

                  }
            }
        }
        echo $res;
        break;

    case 'showDeliveryMode':
        for($i=0;$i<sizeof($_SESSION['cart']['vendors']);$i++){
                $d=$_SESSION['cart']['vendors'][$i]['vendor']->getJSONDeliveryByCountry($_POST['id_country']);
                switch(sizeof($d)){
                    case '0':
                        $_SESSION['cart']->changeDeliveryMode($i,null);
                        break;
                    case '1':
                        $_SESSION['cart']->changeDeliveryMode($i,$d[0]);
                        break;
                }
                $vendors[] = array("Vendor"=>$_SESSION['cart']['vendors'][$i]['vendor']->getArray(), "DeliveryMode" => $d);

        }
        $datas=array("vendors"=>$vendors, "total"=>$_SESSION['cart']->getRecalculateTotal() );
        echo json_encode($datas);
        break;

    case 'changeDeliveryMode':
        $flag=false;
        for($i=0;$i<sizeof($_SESSION['cart']['vendors']);$i++){
            if($_SESSION['cart']['vendors'][$i]['vendor']['id']==$_POST['id_vendor']) {
                $iterator=$i;
                $d=$_SESSION['cart']['vendors'][$i]['vendor']->getJSONDeliveryByCountry($_SESSION['cart']['country']['id']);

                $dobj=$_SESSION['cart']['vendors'][$i]['vendor']->getDeliveryByCountry($_SESSION['cart']['country']['id']);
                foreach ($dobj as $obj){
                    if($obj['id']==$_POST['id_mode']){
                        $_SESSION['cart']->changeDeliveryMode($i,$obj);
                        $flag=true;
                    }
                }
                foreach ($d as $deliv){
                    if($deliv['id']==$_POST['id_mode']){
                        $datas = array("DeliveryMode" => $deliv, "total"=>$_SESSION['cart']->getRecalculateTotal());
                    }
                }
                
            }
        }
        if($flag==false){ $_SESSION['cart']->changeDeliveryMode($iterator,null);$datas = array("DeliveryMode" => null, "total"=>$_SESSION['cart']->getRecalculateTotal());}
        echo json_encode($datas);
        break;

     case 'changeBonus':
         ($_POST['value']==1)?$_SESSION['cart']['bonus']=1:$_SESSION['cart']['bonus']=0;
         break;

     case 'supportAssociation':
         if($_POST['value'] == 1) {
             $_SESSION['cart']->addSupportAssociation($_POST['association']);
         } else {
             $_SESSION['cart']->deleteSupportAssociation($_POST['association']);
         }
         break;

     case 'verifReductionCode':
         if(isset($_SESSION['cart']['sponsorship'])){ $datas = array("error"=>'both');echo json_encode($datas); break;}
         $code = ReductionCode::get($_POST['code']);
         if($code!=false) {
             $valid = $code->isValid();
             if($valid==true){
                 $totalWS = $_SESSION['cart']->getRecalculateTotal(false);
                 if($totalWS=='0,00'){
                     $datas = array("error"=>'delivery');
                 }else {
                    if($code['list']['min_achat']<=$totalWS){
                     $_SESSION['cart']['reduction_code']=$code;
                     $datas = array("error"=>false, "total"=>$_SESSION['cart']->getRecalculateTotal(), "value"=>$code['list']['reduction']);
                    }
                    else $datas = array("error"=>'min_achat', "total"=>$totalWS, "value"=>$code['list']['reduction'], "min"=>$code['list']['min_achat']);
                 }
             }
             else {
                 switch($valid){
                     case '1':
                         array("error"=>'not_available');
                         break;
                     case '2':
                         array("error"=>'obsolete');
                         break;
                 }
             }
         } else $datas = array("error"=>'not_exist');
         echo json_encode($datas);
         break;
      case 'verifSponsorship':
          if(isset($_SESSION['cart']['reduction_code'])){ $datas = array("error"=>'both');echo json_encode($datas); break;}
          if(stripos($_POST['amount'], ',')) $_POST['credit']=str_replace(',', '.', $_POST['amount']);
          if(!is_numeric($_POST['amount'])){ $datas = array("error"=>'seizure');echo json_encode($datas); break;}
          else {
               $_POST['amount'] = number_format($_POST['amount'],2,'.',' ');
               if(floatval($_POST['amount'])>50){ $datas = array("error"=>'fifty');echo json_encode($datas); break;}
               if(floatval($_POST['amount'])>$_SESSION['user']['credit_parrainage']){ $datas = array("error"=>'sup');echo json_encode($datas); break;}
               $totalWS=$_SESSION['cart']->getRecalculateTotal(false);
               if($totalWS=='0,00'){ $datas = array("error"=>'delivery');echo json_encode($datas); break;}
               if($totalWS<$_SESSION['cart']->getMinAmount(floatval($_POST['amount']))){ $datas = array("error"=>'min_achat', "total"=>$totalWS, "value"=>$_POST['amount'], "min"=>$_SESSION['cart']->getMinAmount(floatval($_POST['amount'])));echo json_encode($datas); break;}
               $_SESSION['cart']['sponsorship']= $_POST['amount'];
               $datas = array("error"=>'no', "total"=>$_SESSION['cart']->getRecalculateTotal(), "value"=>$_SESSION['cart']['sponsorship']);
          }
          echo json_encode($datas);
          break;
      case 'deliveryAddress':
          $_SESSION['cart']['deliveryAddress']=($_POST['address']=='true')?1:0;
          $_SESSION['cart']->addDeliveryAddress();
          break;
      case 'displayError':
          
            if(isset($_POST['id_vendors'])){
                $vendors=array();
                $id_vendors = explode("&", $_POST['id_vendors']);
                foreach($id_vendors as $id_vendor) $vendors[]=new Vendor($id_vendor);
                $tpl->assign("vendors", $vendors);
            }
        
        $tpl->assign("error", $_POST['error']);
        
        $tpl->display("ajax/cart_error.tpl");
         break;
 
}

?>